<?php

/**
 * 开发
 * ============================================================================
 * * 版权所有 2012 铭迈（上海）实业有限公司，并保留所有权利。
 * 网站地址: http://www.mingmailtd.com/；
 * ----------------------------------------------------------------------------
 * 仅用于公司内部人员使用。
 * ============================================================================
 * $Author: zhangmingming $
 * $Id: lib_smart.php 2012/6/27 15:34:03 $
 */

define('IN_MMOA', true);

require(dirname(__FILE__) . '/includes/init.php');

$username = trim($_SESSION['username']);

//默认显示我的建议
if ($_REQUEST['act'] == '')
{
	$sql = "SELECT id FROM " . $mmoa->table('employees') . " WHERE e_name = '$username'";
	$id = $db->getOne($sql);
	
	$d = develop_show($id);
	$smarty->assign('develop', $d);

	$smarty->assign('mm_show', 'MyDevelop');
	
	clear_caches();
	$smarty->display('develop.tpl');
}
elseif ($_REQUEST['act'] == 'add')
{
	$smarty->assign('nowdate', date("Y-m-d"));
	$smarty->assign('mm_show', 'AddDevelop');
	
	clear_caches();
	$smarty->display('develop.tpl');
}
elseif ($_REQUEST['act'] == 'update')
{
	$no = create_dev_no(); //echo $no; exit; //dev_no
	$name  = $_POST['devname']; //dev_name
	$desc = $_POST['describe'];  //dev_desc
	$cate = $_POST['category']; //dev_category
	$level = $_POST['level']; //dev_level
	$deadline = $_POST['deadline'];  //echo $deadline; exit; //dev_deadline

	//echo $no . ' - ' . $name . ' - ' . $desc . ' - ' . $cate . ' - ' . $level . ' - ' . $deadline ;  //exit();
	
	$file = $_FILES['devfile']; //print_r($file); //exit();
	$upload_dir = 'upload/dev/';
	$upload_name = $upload_dir.$file['name'];
	
	if (!empty($file['name'])){
	if($file['error'] > 0){
		echo '<meta charset="utf-8">';
		echo '文件上传错误：'. $file['error'];
		exit();
	} else {
		//echo '文件名：' . $_FILES['devfile']['name']  . '<br />';
		//echo '类型：' .  $file['type'] . '<br />';
		//echo '大小：' .  $file['size'] . '<br />';
		if (file_exists($upload_name)){
			echo '<meta charset="utf-8">';
			echo $file['name'] . '文件已经存在。';
			exit();
		} else {
			move_uploaded_file($file['tmp_name'], $upload_name);
			//echo '文件已经被存储到：' . $upload_name;
		}
		
		$devfile = $upload_name;
	}
	}
	
	if(!empty($no)){
	  $sql = "SELECT id FROM " . $mmoa->table('employees') . " WHERE e_name = '$username'";
	  $eid = $db->getOne($sql);
	  
	  $date = date("Y-m-d");
	  
	  $sqlu = "INSERT INTO ". $mmoa->table('development') ."(`dev_no`, `dev_name`, `dev_desc`, `dev_level`, `dev_category`, `dev_date`, `dev_deadline`, `dev_seid`, `dev_file`) VALUES ('$no','$name','$desc','$level','$cate','$date','$deadline','$eid','$devfile')";
	  
	  if($db->query($sqlu)){
	  	// 提交成功
	    echo '<meta charset="utf-8">';
	  	echo '提交成功';
	  	//header("Location: develop.php");
	  } else {
	    echo '<meta charset="utf-8">';
	  	echo '提交失败';
	  	//header("Location: develop.php?act=add");
	  }
	}
}

// 审核开发
elseif ($_REQUEST['act'] == 'audit')
{
	//echo 'audit';
	$sql = "SELECT * FROM " . $mmoa->table('development') . " WHERE 1 ORDER BY id DESC";
	$d = $db->getAll($sql);
	for ($i=0; $i<count($d); $i++){
		$seid = $d[$i]['dev_seid'];
		$eid = $d[$i]['dev_eid'];
		$shenid = $d[$i]['dev_shenid'];
		if ($seid){
			$sql = "SELECT e_name FROM " . $mmoa->table('employees') . " WHERE id=$seid";
			$sname = $db->getOne($sql);
			
			$d[$i]['sname'] = $sname;
		}
		if ($eid){
			$sql = "SELECT e_name FROM " . $mmoa->table('employees') . " WHERE id=$eid";
			$name = $db->getOne($sql);
				
			$d[$i]['devname'] = $name;
		}
		if ($shenid){
			$sql = "SELECT e_name FROM " . $mmoa->table('employees') . " WHERE id=$shenid";
			$shenname = $db->getOne($sql);
				
			$d[$i]['shenname'] = $shenname;
		}
		
		// 计算延迟天数
		$complete = $d[$i]['dev_complete'];
		if (!empty($complete) && $complete != '0000-00-00'){
			$times = strtotime($complete) - strtotime($d[$i]['dev_deadline']);
			$d[$i]['delay'] = round($times/3600/24);
		} else {
			$d[$i]['delay'] = '';
		}
		
		// 处理时间
		$d[$i]['dev_date'] = substr($d[$i]['dev_date'],5);
		$d[$i]['dev_distri'] = substr($d[$i]['dev_distri'],5);
		$d[$i]['dev_deadline'] = substr($d[$i]['dev_deadline'],5);
		$d[$i]['dev_complete'] = substr($d[$i]['dev_complete'],5);
	}
	$smarty->assign('develop', $d);
	
	$smarty->assign('mm_show', 'audit');
	
	clear_caches();
	$smarty->display('develop.tpl');
}
elseif ($_REQUEST['act'] == 'modaudit')
{
	//echo 'ok';
	$did = $_GET['did'];
	$sql = "SELECT * FROM " . $mmoa->table('development') . " WHERE id = $did";
	$d = $db->getRow($sql);
	
	$smarty->assign('develop', $d);
	$smarty->assign('did', $did);
	
	$sqle = "SELECT id, e_name FROM " . $mmoa->table('employees') . " WHERE 1";
	$e = $db->getAll($sqle);  //print_r($e);
	
	$smarty->assign('employees', $e);  
	
	$sqls = "SELECT id, s_eid, s_content FROM " . $mmoa->table('suggest') . " WHERE 1";
	$s = $db->getAll($sqls);  //print_r($e);
	
	$smarty->assign('suggest', $s);
	
	$smarty->assign('mm_show', 'modaudit');
	
	clear_caches();
	$smarty->display('develop.tpl');
}
elseif ($_REQUEST['act'] == 'modauditu')
{
	//echo 'ok';
	// 审核开发id号
	$did = $_POST['did'];
	// 更新开发信息
	$sid =  isset($_POST['suggestid']) ? trim($_POST['suggestid']) : '';
	$name = isset($_POST['devname']) ? trim($_POST['devname']) : '';
	$desc = isset($_POST['describe']) ? trim($_POST['describe']) : '';
	$status = isset($_POST['status']) ? trim($_POST['status']) : 0;
	$category = isset($_POST['category']) ? trim($_POST['category']) : 0;
	$level = isset($_POST['level']) ? trim($_POST['level']) : 0;
	$eid = isset($_POST['eid']) ? trim($_POST['eid']) : '';
	$shenid = isset($_POST['shenid']) ? trim($_POST['shenid']) : '';
	$distri = isset($_POST['distri']) ? trim($_POST['distri']) : '';
	$deadline = isset($_POST['deadline']) ? trim($_POST['deadline']) : '';
	$complete = isset($_POST['complete']) ? trim($_POST['complete']) : '';
	$point = isset($_POST['point']) ? trim($_POST['point']) : '';
	$proof = isset($_POST['proof']) ? trim($_POST['proof']) : '';
	$score = isset($_POST['score']) ? trim($_POST['score']) : '';
	$verity = isset($_POST['verity']) ? trim($_POST['verity']) : 0;
	
	//echo $_POST['note']; exit();
	
	if ($sid){
		$sql = "SELECT s_eid FROM " . $mmoa->table('suggest') . " WHERE id = $sid";
		$seid = $db->getOne($sql);
	} else {
		$seid = '';
	}
	
	if ($did){
		$sql = "UPDATE "  . $mmoa->table('development') . "
		SET `dev_sid` = '$sid',
		`dev_seid` = '$seid',
		`dev_name` = '$name',
		`dev_desc` = '$desc',
		`dev_status` = '$status',
		`dev_category` = '$category',
		`dev_level` = '$level',
		`dev_eid` = '$eid',
		`dev_shenid` = '$shenid',
		`dev_distri` = '$distri',
		`dev_deadline` = '$deadline',
		`dev_complete` = '$complete',
		`dev_point` = '$point',
		`dev_proof` = '$proof',
		`dev_score` = '$score',
		`dev_isverity` = '$verity'
		WHERE id = $did";
		if ($db->query($sql)){
			$ename = $_SESSION['username'];
			$devid = $did;
			$note =  isset($_POST['note']) ? trim($_POST['note']) : '';
			date_default_timezone_set('Asia/Shanghai');
			$time = date('Ymdhis');
			$sqll = "INSERT INTO ". $mmoa->table('log') ."(`ename`, `devid`, `note`,time) VALUES ('$ename','$devid','$note','$time')";
			$db->query($sqll);
			
			echo '<meta charset="utf-8">';
			echo '审核 / 修改成功';
		} else {
			echo '<meta charset="utf-8">';
			echo '审核 / 修改失败';
		}
	}
	
	
}
// 删除开发
elseif ($_REQUEST['act'] == 'del')
{
	// 删除开发编号
	$did = $_GET['did'];
	if ($did){
		$sql = "DELETE FROM " . $mmoa->table('development') . " WHERE id = $did";
		if ($db->query($sql)){
			echo '<meta charset="utf-8">';
			echo '删除成功';
		} else {
			echo '<meta charset="utf-8">';
			echo '删除失败';
		}
	} else {
		echo '<meta charset="utf-8">';
		echo '删除失败';
	}
}

// 负责开发
elseif ($_REQUEST['act'] == 'project')
{
	//echo 'project';
	$ename = $_SESSION['username'];
	$sql = "SELECT id FROM " . $mmoa->table('employees') . " WHERE e_name = '$ename'";
	$eid = $db->getOne($sql);
	
	$sqld = "SELECT * FROM " . $mmoa->table('development') . " WHERE dev_eid = $eid";
	$d = $db->getAll($sqld);
	for ($i=0; $i<count($d); $i++){
		// 计算延迟天数
		$complete = $d[$i]['dev_complete'];
		if (!empty($complete) && $complete != '0000-00-00'){
			$times = strtotime($complete) - strtotime($d[$i]['dev_deadline']);
			$d[$i]['delay'] = round($times/3600/24);
		} else {
			$d[$i]['delay'] = '';
		}
		
		// 处理时间
		$d[$i]['dev_date'] = substr($d[$i]['dev_date'],5);
		$d[$i]['dev_distri'] = substr($d[$i]['dev_distri'],5);
		$d[$i]['dev_deadline'] = substr($d[$i]['dev_deadline'],5);
		$d[$i]['dev_complete'] = substr($d[$i]['dev_complete'],5);
	}
	
	$smarty->assign('develop', $d);
	
	$smarty->assign('mm_show', 'project');
	
	clear_caches();
	$smarty->display('develop.tpl');
}
// 负责开发项目提交
elseif ($_REQUEST['act'] == 'submit')
{
	//echo 'ok';
	$did = $_GET['did'];	
	$sql = "SELECT * FROM " . $mmoa->table('development') . " WHERE id = $did";
	$d = $db->getRow($sql);
	
	$smarty->assign('develop', $d);
	$smarty->assign('did', $did);
	
	$smarty->assign('mm_show', 'submitdev');
	
	clear_caches();
	$smarty->display('develop.tpl');
	
}
elseif ($_REQUEST['act'] == 'submitu')
{
	$did = $_POST['did'];
	$status = isset($_POST['status']) ? trim($_POST['status']) : 0;
	
	if ($did){
		$sql = "UPDATE "  . $mmoa->table('development') . "
		SET `dev_status` = '$status'
		WHERE id = $did";
		if ($db->query($sql)){
			echo '<meta charset="utf-8">';
			echo '提交成功';
		} else {
			echo '<meta charset="utf-8">';
			echo '提交失败';			
		}
	} else {
		echo '<meta charset="utf-8">';
		echo '提交失败';
	}
}
// 统计开发
elseif ($_REQUEST['act'] == 'tongji')
{
	$eid = isset($_POST['eid']) ? trim($_POST['eid']) : '';
	
	$develop = develop_tongji($eid); //print_r($develop);
	
	
	$smarty->assign('develop', $develop);
	
	$smarty->assign('mm_show', 'tongji');
	
	clear_caches();
	$smarty->display('develop.tpl');
}
// 导出表格
elseif ($_REQUEST['act'] == 'tongji_excel')
{
	//echo 'ok';
	
	$eid = isset($_POST['eid']) ? trim($_POST['eid']) : '';
	$start_date = isset($_POST['start_date']) ? trim($_POST['start_date']) : '';
	$end_date = isset($_POST['end_date']) ? trim($_POST['end_date']) : '';
	
	
	$develop = develop_tongji($start_date,$end_date);
	
	if (!empty($develop)) {
		require_once 'includes/PHPExcel/Classes/PHPExcel.php';


// Create new PHPExcel object
$objPHPExcel = new PHPExcel();

// Set document properties
$objPHPExcel->getProperties()->setCreator("MMOA")
							 ->setLastModifiedBy("MMOA")
							 ->setTitle("开发统计")
							 ->setSubject("开发统计")
							 ->setDescription("导出开发统计")
							 ->setKeywords("developments excel")
							 ->setCategory("developments result file");


// Add some data
$objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A1', '开发编号')
            ->setCellValue('B1', '开发者')
            ->setCellValue('C1', '审核者')
            ->setCellValue('D1', '提出者')
            ->setCellValue('E1', '开发名称')
            ->setCellValue('F1', '开发描述')
            ->setCellValue('G1', '状态')
            ->setCellValue('H1', '提出时间')
            ->setCellValue('I1', '分配时间')
            ->setCellValue('J1', '截止时间')
            ->setCellValue('K1', '完成时间')
            ->setCellValue('L1', '延迟天数')
            ->setCellValue('M1', '需求分数')
            ->setCellValue('N1', '校对得分')
            ->setCellValue('O1', '审核得分');

// Miscellaneous glyphs, UTF-8
for ($i=0; $i < count($develop); $i++) {
	$row = $i+2;
	$objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A'.$row, $develop[$i]['dev_no'])
            ->setCellValue('B'.$row, $develop[$i]['devname'])
            ->setCellValue('C'.$row, $develop[$i]['shenname'])
            ->setCellValue('D'.$row, $develop[$i]['sname'])
            ->setCellValue('E'.$row, $develop[$i]['dev_name'])
            ->setCellValue('F'.$row, $develop[$i]['dev_desc'])
            ->setCellValue('G'.$row, $develop[$i]['dev_status'])
            ->setCellValue('H'.$row, $develop[$i]['dev_date'])
            ->setCellValue('I'.$row, $develop[$i]['dev_distri'])
            ->setCellValue('J'.$row, $develop[$i]['dev_deadline'])
            ->setCellValue('K'.$row, $develop[$i]['dev_complete'])
            ->setCellValue('L'.$row, $develop[$i]['delay'])
            ->setCellValue('M'.$row, $develop[$i]['dev_point'])
            ->setCellValue('N'.$row, $develop[$i]['dev_proof'])
            ->setCellValue('O'.$row, $develop[$i]['dev_score']);
}


// Rename worksheet
$objPHPExcel->getActiveSheet()->setTitle('开发统计');


// Set active sheet index to the first sheet, so Excel opens this as the first sheet
$objPHPExcel->setActiveSheetIndex(0);


// Redirect output to a client’s web browser (Excel2007)
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="develop.xlsx"');
header('Cache-Control: max-age=0');

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output');
exit;
	}
	
}

// 显示操作记录
elseif ($_REQUEST['act'] == 'showlog')
{
	$devid = isset($_GET['did']) ? trim($_GET['did']) : ''; 
	if (!empty($devid)) {		
		$sql = "SELECT * FROM " . $mmoa->table('log') . " WHERE devid = $devid ORDER BY id DESC";	
		$log = $db->getAll($sql);
	}
	
	/*
	for($i=0; $i < count($log); $i++){
		//$time = $log[$i]['time'];
		//$log[$i]['time'] = localtime($time);
	}*/
	
	$smarty->assign('log', $log);
	
	$smarty->assign('mm_show', 'showlog');
	
	clear_caches();
	$smarty->display('develop.tpl');
}




//********************-- FUNCTIONS
function develop_show($d)
{
	$sql = "SELECT * FROM " . $GLOBALS['mmoa']->table('development') .
	" where dev_seid = '" . $d . "' ORDER BY id DESC";
	$res = $GLOBALS['db']->getAll($sql);
	
	for($i = 0; $i < count($res); $i++)
	{
		$id = $res[$i]['dev_eid'];
		if(!empty($id)){
			$sql1 = "SELECT e_name FROM " . $GLOBALS['mmoa']->table('employees') .
			       " where id = '" . $id . "'";
			$name = $GLOBALS['db']->getOne($sql1);
			$res[$i]['devname'] = $name;
		}
		
		// 处理时间
		$res[$i]['dev_date'] = substr($res[$i]['dev_date'],5);
		$res[$i]['dev_distri'] = substr($res[$i]['dev_distri'],5);
		$res[$i]['dev_deadline'] = substr($res[$i]['dev_deadline'],5);
		$res[$i]['dev_complete'] = substr($res[$i]['dev_complete'],5);
		
	}
	
	return $res;
}

function develop_tongji($start_date,$end_date)
{
	$where = '';
	if (!empty($start_date) && !empty($end_date)) {
		$where .=" dev_complete >= '". $start_date . "' AND dev_complete <= '" . $end_date . "'";
	} else {
		$where = ' 1 ';
	}
	$sql = "SELECT * FROM " . $GLOBALS['mmoa']->table('development') .
	" where $where ORDER BY id DESC";
	$res = $GLOBALS['db']->getAll($sql);
	
	for($i = 0; $i < count($res); $i++)
	{
		$id = $res[$i]['dev_eid'];
		$shenid = $res[$i]['dev_shenid'];
		$seid = $res[$i]['dev_seid'];
		if(!empty($id)){
			$sql1 = "SELECT e_name FROM " . $GLOBALS['mmoa']->table('employees') .
			       " where id = '" . $id . "'";
			$name = $GLOBALS['db']->getOne($sql1);
			$res[$i]['devname'] = $name;
		}
		if(!empty($shenid)){
			$sql1 = "SELECT e_name FROM " . $GLOBALS['mmoa']->table('employees') .
			       " where id = '" . $shenid . "'";
			$name = $GLOBALS['db']->getOne($sql1);
			$res[$i]['shenname'] = $name;
		}
		if(!empty($seid)){
			$sql1 = "SELECT e_name FROM " . $GLOBALS['mmoa']->table('employees') .
			       " where id = '" . $seid . "'";
			$name = $GLOBALS['db']->getOne($sql1);
			$res[$i]['sname'] = $name;
		}
		
		 // 计算延迟天数
		$complete = $res[$i]['dev_complete'];
		if (!empty($complete) && $complete != '0000-00-00'){
			$times = strtotime($complete) - strtotime($res[$i]['dev_deadline']);
			$res[$i]['delay'] = round($times/3600/24);
		} else {
			$res[$i]['delay'] = '';
		}
		
		
		// 处理时间
		$res[$i]['dev_date'] = substr($res[$i]['dev_date'],5);
		$res[$i]['dev_distri'] = substr($res[$i]['dev_distri'],5);
		$res[$i]['dev_deadline'] = substr($res[$i]['dev_deadline'],5);
		$res[$i]['dev_complete'] = substr($res[$i]['dev_complete'],5);
	
	  
	}
	
	return $res;
}
